package com.wgts.wgts.mvp.view.activity.wgtshome;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerWgtsNomalMarketActivityComponent;
import com.wgts.wgts.dagger.module.WgtsNomalMarketActivityModule;
import com.wgts.wgts.mvp.presenter.IWgtsNomalMarketActivityPresenter;
import com.wgts.wgts.mvp.view.IWgtsNomalMarketActivityView;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.mvp.view.activity.homapge.BannerViewHolder;
import com.wgts.wgts.mvp.view.activity.webactivity.WebActivity;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.netmodle.resp.WgtsProductListEntity;
import com.wgts.wgts.tools.Tools;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WgtsNomalMarketActivity extends BaseActivity implements IWgtsNomalMarketActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.mz_banner)
    MZBannerView mzBanner;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    @Inject
    IWgtsNomalMarketActivityPresenter presenter;

    private WgtsProductAdapter adapter;

    private List<WgtsProductListEntity> srcDate;

    @Override
    public void init() {
        DaggerWgtsNomalMarketActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .wgtsNomalMarketActivityModule(new WgtsNomalMarketActivityModule(this))
                .build()
                .inject(this);
        srcDate = new ArrayList<>();
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.wgts_nomal_market_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

//        DisplayMetrics outMetrics = Tools.metrics(this);
//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(outMetrics.widthPixels, (int) (outMetrics.widthPixels * 0.45));
//        mzBanner.setLayoutParams(layoutParams);
//        Tools.setViewMargin(mzBanner, true, 0, 0, 44, 10);

        presenter.initDataView();
        presenter.initProduct("NORMAL");
        includeTitle.setText("微广商城");
        adapter = new WgtsProductAdapter(this, srcDate);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setAdapter(adapter);

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.moreProduct();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.initProduct("NORMAL");
            }
        });

        adapter.setOnClickListener(new WgtsProductAdapter.OnClickListener() {
            @Override
            public void onClick(View view, int index) {
                Intent intent = new Intent(WgtsNomalMarketActivity.this, JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.PRODUCT_ID, srcDate.get(index).productId);
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.back_btn_ly)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onError(int status, String msg) {
        netErrorLy.setVisibility(View.VISIBLE);
    }


    @Override
    public void initProductList(List<WgtsProductListEntity> entities, boolean noMore) {
        refreshLayout.finishRefresh();
        srcDate.clear();
        srcDate.addAll(entities);
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
        refreshLayout.setEnableLoadmore(!noMore);
    }

    @Override
    public void moreProductList(List<WgtsProductListEntity> entities, boolean noMore) {
        refreshLayout.finishLoadmore();
        srcDate.addAll(entities);
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
        refreshLayout.setEnableLoadmore(!noMore);
    }

    @Override
    public void initResult(List<AppModleEntity> appModleEntities) {
        refreshLayout.finishRefresh();

        Log.d("", "");
        for (AppModleEntity entity : appModleEntities) {
            if (entity.type.equals("INDEX_BANNER")) {
                initBanner(entity.result);
            }

        }
    }

    private void initBanner(final List<AppModleEntity.AppModleResult> banners) {


        DisplayMetrics outMetrics = Tools.metrics(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(outMetrics.widthPixels, (int) (outMetrics.widthPixels * 0.45));
        mzBanner.setLayoutParams(layoutParams);
        //Tools.setViewMargin(mzBanner, true, 0, 0, 44, 10);

        mzBanner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int i) {

                Intent intent = null;

                if (banners.get(i).actionUrlType.equals("0")) {
                    intent = new Intent(WgtsNomalMarketActivity.this, WebActivity.class);
                    intent.putExtra(WebActivity.ACTION_URL, banners.get(i).actionUrl);
                    intent.putExtra(WebActivity.TITLE, banners.get(i).name);

                } else {
                    try {
                        JSONObject jsonObject = new JSONObject(banners.get(i).actionUrl);
                        switch (jsonObject.getString("code")) {
                            case "5":
                                intent = new Intent(WgtsNomalMarketActivity.this, JDGoodinfoActivity.class);
                                intent.putExtra(JDGoodinfoActivity.PRODUCT_ID, jsonObject.getString("param"));
                                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                                break;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                if (intent != null) {
                    startActivity(intent);
                }
            }
        });

        mzBanner.setPages(banners, new MZHolderCreator() {
            @Override
            public MZViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        mzBanner.start();


    }
}
